﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using compshop.Infrastructure.CompShopDataService;
using compshop.Modules.OrdersToSupplier.Presenter;

namespace compshop.Modules.OrdersToSupplier.Views
{
    /// <summary>
    /// Interaction logic for CreateOrderToSupplier.xaml
    /// </summary>
    public partial class CreateOrderToSupplierDialog : Window, IViewOrdersToSupplier
    {
        private OrderToSupplier order;
        private Employee manager=new Employee();

        public OrdersToSupplierPresenter Presenter { get; set; }

        public OrderToSupplier Order
        {
            get { return order; }
            set { order = value; }
        }

        public CreateOrderToSupplierDialog(OrderToSupplier order)
        {
            InitializeComponent();
            this.order = order;
        }


        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (order.OrderToSupplierID != 0)
                InitFields(true);
            else
                InitFields(false);
        }


        private void InitFields(bool isNewOrder)
        {
            manager = Presenter.EmployeeList[0];
            if (isNewOrder)
            {
                lblNumber.Content = order.OrderToSupplierID;
                lblDate.Content = order.CreationDate;
                lblManager.Content = order.Employee.LastName;
                lblStatus.Content = (new ConvertSupplierData()).Convert(order.Status, null, null, null);               
            }
            else
            {
                lblDate.Content = DateTime.Now;
                lblManager.Content = manager.LastName;
                lblStatus.Content = (new ConvertSupplierData()).Convert(1, null, null, null);
            }

            lvProductList.ItemsSource = order.SupplierGeneralProductLists;
            cboxSupplier.ItemsSource = Presenter.SupplierList;
            cboxSupplier.DisplayMemberPath = "Name";
            cboxSupplier.SelectedIndex = 0;
        }


        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            if (true)
            {
                Supplier supp=(Supplier)cboxSupplier.SelectedItem;
                if(supp==null)
                {
                    MessageBox.Show(Resource.NoSelectedSupplier);
                    return;
                }
                order.Supplier = supp;
                order.SupplierID = supp.SupplierID;
                if (order.OrderToSupplierID == 0)
                {
                    order.PurchasingManagerID = manager.EmployeeID;
                    order.Employee = manager;
                }
                order.Status =(int) (new ConvertSupplierData()).ConvertBack(lblStatus.Content, null, null, null);
                order.CreationDate =(DateTime) lblDate.Content;
                DialogResult = true;
                Close();
            }
        }

        private void btnAddProduct_Click(object sender, RoutedEventArgs e)
        {
            
        }

        private void btnDelProduct_Click(object sender, RoutedEventArgs e)
        {

        }

        private void cboxSupplier_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

    }
}
